﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public  class SteelRebar
    {
        public static void DrawUpperRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, List<Point3d> ptTopUpper, double radius, int num, double tH = 100, double tL = 10,double thCap=15)
        {         
            #region 
            #region 网格绘制
            for (int i = 0; i < ptTop.Count; i++)
            {                         
                PLPloter.AddLine(db,ptTop[i].C2D(0, thCap), ptTopUpper[i].C2D(), ref ext, "H钢筋");
                PLPloter.AddLine(db,ptTop[i].C2D(0, 0), ptTop[i].C2D(0, thCap), ref ext, "H钢筋");
            }
            for (int i = 0; i < num; i++)
            {
                double offset = tL / num;
                double interval = tH / (num-1);
                PLPloter.AddLine(db,pt.C2D(-radius - offset*i, interval * i + thCap), pt.C2D(radius + offset * i, interval * i + thCap), ref ext, "H钢筋");
            }
            #endregion
            #endregion

        }

        public static void DrawRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, double radius, double Height = 5000, double interval = 100, double arcRadius = 1000,double tH = 15)
        {
            //double tH = 20 * 10, tH1 = 50 * 10, tH2 = 200 * 10;
            
            #region 
            int num = (int)(Height / interval);
           
            #region  特殊处理

            #endregion
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                #region  圆弧

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                //Polyline Line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };

                double arcAngle = 60;

                Arc arc1 = new Arc(pt.C3D(-arcRadius + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)), -Height + arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);

                Arc arc2 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(Height + arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);

                Arc arc3 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(Height - arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);

                List<Point2d> list = new List<Point2d>();
                //Line.AddVertexAt(0, pt.C2D(-radius, 0), 0, 0, 0);
                //Line.AddVertexAt(0, pt.C2D(radius, 0), 0, 0, 0);
                list.Add(pt.C2D(-radius, 0));
                list.Add(pt.C2D(radius, 0));
                for (int i = 0; i < num+1; ++i)
                {
                    Point2d pt1 = list[list.Count - 1].C2D(-radius*2, -interval * 0.5);
                    list.Add(pt1.C2D());
                    Point2d pt2 = list[list.Count - 1].C2D(radius*2, -interval * 0.5);
                    list.Add(pt2.C2D());
                    //Line.AddVertexAt(0, pt1, 0, 0, 0);
                    //Line.AddVertexAt(0, pt2, 0, 0, 0);
                }
                for (int i = 1; i < list.Count; i++)
                {
                    List<Point3d> nodeLst = new List<Point3d>();
                    List<Point3d> nodeLst1 = new List<Point3d>();
                    Point3d pt1 = list[i - 1].C3D();
                    Point3d pt2 = list[i].C3D();
                    Line line = new Line(pt1, pt2) { LayerId = lt["H钢筋"] }; 
                    MathExtensions.LineIntersectArcNew(line, arc2, ref nodeLst);
                    MathExtensions.LineIntersectArcNew(line, arc1, ref nodeLst1);
                    if (nodeLst.Count > 0)
                    {
                       
                        nodeLst = nodeLst.OrderBy(o => o.X).ToList();
                        if (nodeLst.Count > 1)
                        {
                            if (pt1.X <= nodeLst[0].X && pt2.X >= nodeLst[1].X)
                            {
                                line = new Line(pt1, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt2.X > nodeLst[1].X)
                                {
                                    line = new Line(nodeLst[1].C3D(), pt2) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                            else if (pt2.X <= nodeLst[0].X && pt1.X >= nodeLst[1].X)
                            {
                                line = new Line(pt2, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt1.X > nodeLst[1].X)
                                {
                                    line = new Line(nodeLst[1].C3D(), pt1) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                        }
                        else
                        {
                            if(pt2.X> pt1.X)
                            {
                                if(nodeLst[0].X- pt1.X> radius&& nodeLst[0].X<= pt2.X&& nodeLst[0].X >= pt1.X)
                                {
                                    line = new Line(pt1, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                            else
                            {
                                if (nodeLst[0].X - pt2.X > radius && nodeLst[0].X <= pt2.X && nodeLst[0].X >= pt1.X)
                                {
                                    line = new Line(pt2, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                        }
                      
                    }
                    else if (nodeLst1.Count > 0)
                    {
                        if (nodeLst1.Count > 1)
                        {
                            nodeLst1 = nodeLst1.OrderBy(o => o.X).ToList();
                            line = new Line(nodeLst1[0], nodeLst1[1].C3D()) { LayerId = lt["H钢筋"] };
                            recorder.AppendEntity(line);
                            tr.AddNewlyCreatedDBObject(line, true);
                        }                     
                    }
                    else
                    {
                        recorder.AppendEntity(line);
                        tr.AddNewlyCreatedDBObject(line, true);
                    }
                }
                #endregion
                #region 网格绘制
                Point3dCollection pts = new Point3dCollection();
                for (int i = 0; i < ptTop.Count; i++)
                {
                    pts.Clear();
                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };
                    // 计算垂直钢筋线与弧线的交点
                    if (ptTop[i].X <= arc1.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {
                        Line line1 = new Line(ptTop[i].C3D(0, tH), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                     
                        MathExtensions.LineIntersectArcNew(line1, arc1, ref node);
                
                        line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);

                        for (int m = 0; m < node.Count; m++)
                        {
                            if ( node[m].Y<=pt.C2D(0, -Height).Y)
                            {
                                line.AddVertexAt(0, node[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                    }
                    else if (arc3.Center.X - arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)) <= ptTop[i].X && ptTop[i].X <= arc3.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {

                        Line line1 = new Line(ptTop[i].C3D(), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arc2, ref node);
                        line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);
                        for (int m = 0; m < node.Count; m++)
                        {
                            if (node[m].Y >= pt.C2D(0, -Height).Y)
                            {
                                line.AddVertexAt(0, node[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                    }
                    recorder.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                }


              
                #endregion

                tr.Commit();
            }
            #endregion

        }

        public static void DrawMidRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, double radius, double Height = 5000, double interval0 = 100, int num0 = 5, double interval = 100,int num=10, double interval1 = 100,int num1=5, double arcRadius = 1000)
        {
            //double tH = 20 * 10, tH1 = 50 * 10, tH2 = 200 * 10;
            double tH = arcRadius;
            #region 
        
            #region  特殊处理

            #endregion
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                #region  圆弧

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                //Polyline Line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };

                double arcAngle = 60;
                double cosH = arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2));

                Arc arcUp1 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, cosH), arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp1.Bounds.Value.MinPoint.C2D(), arcUp1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp1);
                tr.AddNewlyCreatedDBObject(arcUp1, true);


                Arc arcUp3 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp3.Bounds.Value.MinPoint.C2D(), arcUp3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp3);
                tr.AddNewlyCreatedDBObject(arcUp3, true);

                Arc arcUp2 = new Arc(pt.C3D((arcRadius / 2), -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp2.Bounds.Value.MinPoint.C2D(), arcUp2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp2);
                tr.AddNewlyCreatedDBObject(arcUp2, true);

              

                Arc arc1 = new Arc(pt.C3D(-arcRadius + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)), -Height + arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);

                Arc arc2 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(Height + arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);

                Arc arc3 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(Height - arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);
                #endregion

                #region 绘制螺旋筋
                List<Point2d> list = new List<Point2d>();
                //Line.AddVertexAt(0, pt.C2D(-radius, 0), 0, 0, 0);
                //Line.AddVertexAt(0, pt.C2D(radius, 0), 0, 0, 0);
                list.Add(pt.C2D(-radius, interval * 0.5));
                list.Add(pt.C2D(radius, 0));
                for (int i = 0; i < num0; ++i)
                {
                    Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval0 * 0.5);
                    list.Add(pt1.C2D());
                    Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval0 * 0.5);
                    list.Add(pt2.C2D());
                    //Line.AddVertexAt(0, pt1, 0, 0, 0);
                    //Line.AddVertexAt(0, pt2, 0, 0, 0);
                }
                for (int i = 0; i < num ; ++i)
                {
                    Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval * 0.5);
                    list.Add(pt1.C2D());
                    Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval * 0.5);
                    list.Add(pt2.C2D());
                    //Line.AddVertexAt(0, pt1, 0, 0, 0);
                    //Line.AddVertexAt(0, pt2, 0, 0, 0);
                }
                for (int i = 0; i < num1; ++i)
                {
                    Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval1 * 0.5);
                    list.Add(pt1.C2D());
                    Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval1 * 0.5);
                    list.Add(pt2.C2D());
                }
                #endregion


                #region 螺旋筋与剖段圆弧交叉点处理
                for (int i = 1; i < list.Count; i++)
                {

                    List<Point3d> nodeLstUp = new List<Point3d>();
                    List<Point3d> nodeLstUp1 = new List<Point3d>();
                   
                    List<Point3d> nodeLst = new List<Point3d>();
                    List<Point3d> nodeLst1 = new List<Point3d>();
                    Point3d pt1 = list[i - 1].C3D();
                    Point3d pt2 = list[i].C3D();
                    Line line = new Line(pt1, pt2) { LayerId = lt["H钢筋"] };


                    MathExtensions.LineIntersectArcNew(line, arcUp1, ref nodeLstUp);
                    MathExtensions.LineIntersectArcNew(line, arcUp2, ref nodeLstUp1);

                    MathExtensions.LineIntersectArcNew(line, arc2, ref nodeLst);
                    MathExtensions.LineIntersectArcNew(line, arc1, ref nodeLst1);
                    if (nodeLstUp.Count > 0)
                    {

                        nodeLstUp = nodeLstUp.OrderBy(o => o.X).ToList();
                        if (nodeLstUp.Count > 1)
                        {
                            if (pt1.X <= nodeLstUp[0].X && pt2.X >= nodeLstUp[1].X)
                            {
                                line = new Line(pt1, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt2.X > nodeLstUp[1].X)
                                {
                                    line = new Line(nodeLstUp[1].C3D(), pt2) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                            else if (pt2.X <= nodeLstUp[0].X && pt1.X >= nodeLstUp[1].X)
                            {
                                line = new Line(pt2, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt1.X > nodeLstUp[1].X)
                                {
                                    line = new Line(nodeLstUp[1].C3D(), pt1) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                        }
                        else
                        {
                            if (pt2.X > pt1.X)
                            {
                                if (nodeLstUp[0].X - pt1.X > radius && nodeLstUp[0].X <= pt2.X && nodeLstUp[0].X >= pt1.X)
                                {
                                    line = new Line(pt1, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                            else
                            {
                                if (nodeLstUp[0].X - pt2.X > radius && nodeLstUp[0].X <= pt2.X && nodeLstUp[0].X >= pt1.X)
                                {
                                    line = new Line(pt2, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                        }

                    }
                    else if (nodeLstUp1.Count > 0)
                    {
                        if (nodeLstUp1.Count > 1)
                        {
                            nodeLstUp1 = nodeLstUp1.OrderBy(o => o.X).ToList();
                            line = new Line(nodeLstUp1[0], nodeLstUp1[1].C3D()) { LayerId = lt["H钢筋"] };
                            recorder.AppendEntity(line);
                            tr.AddNewlyCreatedDBObject(line, true);
                        }
                    }

                
                   else if (nodeLst.Count > 0)
                    {

                        nodeLst = nodeLst.OrderBy(o => o.X).ToList();
                        if (nodeLst.Count > 1)
                        {
                            if (pt1.X <= nodeLst[0].X && pt2.X >= nodeLst[1].X)
                            {
                                line = new Line(pt1, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt2.X > nodeLst[1].X)
                                {
                                    line = new Line(nodeLst[1].C3D(), pt2) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                            else if (pt2.X <= nodeLst[0].X && pt1.X >= nodeLst[1].X)
                            {
                                line = new Line(pt2, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt1.X > nodeLst[1].X)
                                {
                                    line = new Line(nodeLst[1].C3D(), pt1) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                        }
                        else
                        {
                            if (pt2.X > pt1.X)
                            {
                                if (nodeLst[0].X - pt1.X > radius && nodeLst[0].X <= pt2.X && nodeLst[0].X >= pt1.X)
                                {
                                    line = new Line(pt1, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                            else
                            {
                                if (nodeLst[0].X - pt2.X > radius && nodeLst[0].X <= pt2.X && nodeLst[0].X >= pt1.X)
                                {
                                    line = new Line(pt2, nodeLst[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                        }

                    }
                    else if (nodeLst1.Count > 0)
                    {
                        if (nodeLst1.Count > 1)
                        {
                            nodeLst1 = nodeLst1.OrderBy(o => o.X).ToList();
                            line = new Line(nodeLst1[0], nodeLst1[1].C3D()) { LayerId = lt["H钢筋"] };
                            recorder.AppendEntity(line);
                            tr.AddNewlyCreatedDBObject(line, true);
                        }
                        else
                        {

                        }

                    }
                    else
                    {
                        recorder.AppendEntity(line);
                        tr.AddNewlyCreatedDBObject(line, true);
                    }
                }
                #endregion

              
                #region 主筋绘制 主筋与剖段圆弧交叉点处理
                Point3dCollection pts = new Point3dCollection();
                for (int i = 0; i < ptTop.Count; i++)
                {
                    pts.Clear();
                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };
                    // 计算垂直钢筋线与弧线的交点
                    if (ptTop[i].X <= arc1.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {
                        Line line1 = new Line(ptTop[i].C3D(0, tH), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arc1, ref node);

                        //line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);
                        List<Point3d> nodeUp = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arcUp1, ref nodeUp);
                        for (int m = 0; m < nodeUp.Count; m++)
                        {
                            if (nodeUp[m].Y <= pt.C2D().Y)
                            {
                                line.AddVertexAt(0, nodeUp[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                        for (int m = 0; m < node.Count; m++)
                        {
                            if (node[m].Y <= pt.C2D(0, -Height).Y)
                            {
                                line.AddVertexAt(0, node[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                    }
                    else if (arc3.Center.X - arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)) <= ptTop[i].X && ptTop[i].X <= arc3.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {

                        Line line1 = new Line(ptTop[i].C3D(), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arc2, ref node);
                        //line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);
                        List<Point3d> nodeUp = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arcUp2, ref nodeUp);
                        for (int m = 0; m < nodeUp.Count; m++)
                        {
                            if (nodeUp[m].Y >= pt.C2D().Y)
                            {
                                line.AddVertexAt(0, nodeUp[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                        for (int m = 0; m < node.Count; m++)
                        {
                            if (node[m].Y >= pt.C2D(0, -Height).Y)
                            {
                                line.AddVertexAt(0, node[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                    }
                    recorder.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                }



                #endregion

                tr.Commit();
            }
            #endregion

        }

        public static void DrawBottomRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, List<Point3d> ptBottom, double radius, double Height = 5000, double interval = 100, double bH = 100, double bL = 10, double arcRadius = 1000)
        {
            //double tH = 20 * 10, tH1 = 50 * 10, tH2 = 200 * 10;
            double tH = arcRadius;
            #region 

            #region  特殊处理
            int num = (int)(Height / interval);
            int num1 = (int)((Height-bH) / interval);
            int num2 = (int)((bH) / interval);
            #endregion
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                #region  圆弧

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                //Polyline Line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };

                double arcAngle = 60;
                double cosH = arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2));

                Arc arcUp1 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, cosH), arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp1.Bounds.Value.MinPoint.C2D(), arcUp1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp1);
                tr.AddNewlyCreatedDBObject(arcUp1, true);


                Arc arcUp3 = new Arc(pt.C3D(-arcRadius + arcRadius / 2, -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp3.Bounds.Value.MinPoint.C2D(), arcUp3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp3);
                tr.AddNewlyCreatedDBObject(arcUp3, true);

                Arc arcUp2 = new Arc(pt.C3D((arcRadius / 2), -cosH), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arcUp2.Bounds.Value.MinPoint.C2D(), arcUp2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arcUp2);
                tr.AddNewlyCreatedDBObject(arcUp2, true);

                #endregion

                #region 绘制螺旋筋
                double offset = bL / num2;
                List<Point2d> list = new List<Point2d>();
                List<Point2d> listB = new List<Point2d>();
                //Line.AddVertexAt(0, pt.C2D(-radius, 0), 0, 0, 0);
                //Line.AddVertexAt(0, pt.C2D(radius, 0), 0, 0, 0);
                list.Add(pt.C2D(-radius, interval * 0.5));
                listB.Add(pt.C2D(-radius, interval * 0.5));
                list.Add(pt.C2D(radius, 0));
                listB.Add(pt.C2D(radius, 0));
                for (int i = 0; i < num; ++i)
                {
                    if (i < num1)
                    {
                        Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval * 0.5);
                        list.Add(pt1.C2D());
                        listB.Add(pt1.C2D());
                        Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval * 0.5);
                        list.Add(pt2.C2D());
                        listB.Add(pt2.C2D());
                    }
                    else
                    {
                        listB.Add(listB[listB.Count - 1].C2D(-radius * 2, -interval * 0.5));                
                        Point2d pt1 = listB[listB.Count - 1].C2D(offset * (i - num1)); // list[list.Count - 1].C2D(-radius * 2+ offset*(i- num1), -interval * 0.5);
                        list.Add(pt1.C2D());
                        listB.Add(listB[listB.Count - 1].C2D(radius * 2, -interval * 0.5));
                        Point2d pt2 = listB[listB.Count - 1].C2D(-offset * (i - num1));
                        //Point2d pt2 = list[list.Count - 1].C2D(radius * 2 - offset * (i - num1), -interval * 0.5);
                        list.Add(pt2.C2D());
                    }
                }
            
                list.Add(listB[listB.Count - 1].C2D(-radius * 2+ offset * (num2-1)));
                #endregion


                #region 螺旋筋与剖段圆弧交叉点处理
                for (int i = 1; i < list.Count; i++)
                {

                    List<Point3d> nodeLstUp = new List<Point3d>();
                    List<Point3d> nodeLstUp1 = new List<Point3d>();

                    Point3d pt1 = list[i - 1].C3D();
                    Point3d pt2 = list[i].C3D();
                    Line line = new Line(pt1, pt2) { LayerId = lt["H钢筋"] };


                    MathExtensions.LineIntersectArcNew(line, arcUp1, ref nodeLstUp);
                    MathExtensions.LineIntersectArcNew(line, arcUp2, ref nodeLstUp1);

                    if (nodeLstUp.Count > 0)
                    {

                        nodeLstUp = nodeLstUp.OrderBy(o => o.X).ToList();
                        if (nodeLstUp.Count > 1)
                        {
                            if (pt1.X <= nodeLstUp[0].X && pt2.X >= nodeLstUp[1].X)
                            {
                                line = new Line(pt1, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt2.X > nodeLstUp[1].X)
                                {
                                    line = new Line(nodeLstUp[1].C3D(), pt2) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                            else if (pt2.X <= nodeLstUp[0].X && pt1.X >= nodeLstUp[1].X)
                            {
                                line = new Line(pt2, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                recorder.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line, true);

                                if (pt1.X > nodeLstUp[1].X)
                                {
                                    line = new Line(nodeLstUp[1].C3D(), pt1) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }

                            }
                        }
                        else
                        {
                            if (pt2.X > pt1.X)
                            {
                                if (nodeLstUp[0].X - pt1.X > radius && nodeLstUp[0].X <= pt2.X && nodeLstUp[0].X >= pt1.X)
                                {
                                    line = new Line(pt1, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                            else
                            {
                                if (nodeLstUp[0].X - pt2.X > radius && nodeLstUp[0].X <= pt2.X && nodeLstUp[0].X >= pt1.X)
                                {
                                    line = new Line(pt2, nodeLstUp[0].C3D()) { LayerId = lt["H钢筋"] };
                                    recorder.AppendEntity(line);
                                    tr.AddNewlyCreatedDBObject(line, true);
                                }
                            }
                        }

                    }
                    else if (nodeLstUp1.Count > 0)
                    {
                        if (nodeLstUp1.Count > 1)
                        {
                            nodeLstUp1 = nodeLstUp1.OrderBy(o => o.X).ToList();
                            line = new Line(nodeLstUp1[0], nodeLstUp1[1].C3D()) { LayerId = lt["H钢筋"] };
                            recorder.AppendEntity(line);
                            tr.AddNewlyCreatedDBObject(line, true);
                        }
                    }          
                    else
                    {
                        recorder.AppendEntity(line);
                        tr.AddNewlyCreatedDBObject(line, true);
                    }
                }
                #endregion


                #region 主筋绘制 主筋与剖段圆弧交叉点处理
                Point3dCollection pts = new Point3dCollection();
                for (int i = 0; i < ptTop.Count; i++)
                {
                    pts.Clear();
                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };
                    // 计算垂直钢筋线与弧线的交点
                    if (ptTop[i].X <= arcUp1.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {
                        Line line1 = new Line(ptTop[i].C3D(0, tH), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> nodeUp = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arcUp1, ref nodeUp);
                        for (int m = 0; m < nodeUp.Count; m++)
                        {
                            if (nodeUp[m].Y <= pt.C2D().Y)
                            {
                                line.AddVertexAt(0, nodeUp[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                        line.AddVertexAt(0, ptTop[i].C2D(0,-(Height-bH)), 0, 0, 0);
                        line.AddVertexAt(0, ptBottom[i].C2D(), 0, 0, 0);
                    }
                    else if (arcUp2.Center.X - arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)) <= ptTop[i].X && ptTop[i].X <= arcUp2.Center.X + arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {

                        Line line1 = new Line(ptTop[i].C3D(), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> nodeUp = new List<Point3d>();
                        MathExtensions.LineIntersectArcNew(line1, arcUp2, ref nodeUp);
                        for (int m = 0; m < nodeUp.Count; m++)
                        {
                            if (nodeUp[m].Y >= pt.C2D().Y)
                            {
                                line.AddVertexAt(0, nodeUp[m].C2D(), 0, 0, 0);
                                break;
                            }
                        }
                        line.AddVertexAt(0, ptTop[i].C2D(0, -(Height - bH)), 0, 0, 0);
                        line.AddVertexAt(0, ptBottom[i].C2D(), 0, 0, 0);
                    }
                    recorder.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                }



                #endregion

                tr.Commit();
            }
            #endregion

        }

        #region 主筋大样
        public static void DrawDetails(Database db, ref Extents2d ext, double scale, Point2d pt, Point2d ptB, double TopH, double topL, double BottomH, double BottomL,bool isDim=true)
        {
            RebarPLPloter.CreateBreakLine(db, ref ext, pt.C2D(), ptB, scale, "H钢筋");

            //Line line = PLPloter.AddLine(db,pt.C2D(0,  TopH), pt.C2D(), ref ext, "H钢筋");
            Line lineTop = PLPloter.AddLine(db,pt.C2D(-topL,  TopH), pt.C2D(0, 0), ref ext, "H钢筋");
            //PLPloter.AddLine(db,ptB.C2D(), ptB.C2D(0, -BottomH), ref ext, "H钢筋");
            PLPloter.AddLine(db,ptB.C2D(), ptB.C2D(BottomL, -BottomH), ref ext, "H钢筋");
            if (isDim)
            {
                if (topL != 0)
                {
                    DimPloter.DimRot(db, pt.C3D(0, 0), pt.C3D(-topL, TopH), pt.C3D(-topL - 3 * scale, 0), ref ext, 90, scale, "", Unit.Centimeter, Unit.Centimeter);
                    DimPloter.DimRot(db, pt.C3D(0, 0), pt.C3D(-topL, TopH), pt.C3D(0, TopH + 5 * scale), ref ext, 0, scale, "", Unit.Centimeter, Unit.Centimeter);
                }
                //DimPloter.DimAli(db,pt.C3D(0, 0), pt.C3D(-topL, TopH), pt.C3D(3*scale), ref ext, scale, "", Unit.Centimeter, Unit.Centimeter,1);
                if (BottomL != 0)
                {
                    DimPloter.DimRot(db, ptB.C3D(), ptB.C3D(BottomL, -BottomH), ptB.C3D(-3 * scale, 0), ref ext, 90, scale, "", Unit.Centimeter, Unit.Centimeter);
                    DimPloter.DimRot(db, ptB.C3D(), ptB.C3D(BottomL, -BottomH), ptB.C3D(0, -5 * scale - BottomH), ref ext, 0, scale, "", Unit.Centimeter, Unit.Centimeter);
                }
            }
            //DimPloter.DimAli(db,ptB.C3D(), ptB.C3D(BottomL, -BottomH), ptB.C3D(3 * scale), ref ext,  scale, "", Unit.Centimeter, Unit.Centimeter,1);
            //line = new Line(pt.C3D(0, 20 * 10 + TopH), pt.C3D(0, 20 * 10));
            //lineTop = new Line(pt.C3D(-topL, 20 * 10 + TopH), pt.C3D(0, 20 * 10));
            //DimPloter.DimAng(db,line, lineTop, pt.C3D(0, 20 * 10 + TopH-3.5* scale), ref ext, scale);
        }
        #endregion

   
    }
}
